﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.Data;

using System.Windows.Controls;



namespace HA.ModFlow.Application
{
  public   class DSGrid3dCollecionViewModel:MDTVIViewModel
    {

      public DSGrid3dCollecionViewModel(MDTVIViewModel parent,DSGrid3dCollecion collecion):base(parent,false)
      {
         



          this._collecion = collecion;


          this._imageSource = VMImageSource.result;

          this._isShowCheck = false;


          this._contextMenu = new System.Windows.Controls.ContextMenu();

          MenuItem mi;

          //对于 DSGrid3dCollecion 的右键功能：拷贝（DSGrid3dCollecion），Other没有此功能,MF和MT有，但MF和MT拷贝出来的数据没有
          //删除：所有  DSGrid3dCollecion  都提供
          //删除：other的DSGrid3dCollecion中的DSGrid提供


          mi = new MenuItem();

          mi.Header = "删除数据集";
          mi.Command = Data2Commands.DeleteDSGrid3dCollection ;
          mi.CommandParameter = this._collecion;
          this._contextMenu.Items.Add(mi);


          //不包括 OtherDataSetResult  和 由 ModflowResult 或者 MT3DMSResult 拷贝出来的数据

          if (collecion.Key.ToString().Trim() != "OtherDataSetResult" && (collecion.Key.ToString().Trim() == "ModflowResult" || collecion.Key.ToString().Trim() == "MT3DMSResult"))
          {
              mi = new MenuItem();

              mi.Header = "拷贝数据集";
              mi.Command = Data2Commands.CopyDSGrid3dCollection;
              mi.CommandParameter = this._collecion;
              this._contextMenu.Items.Add(mi);
                           
          }



         



          if (collecion is DSModflow)
          {

              Separator sepa = new Separator();
              this._contextMenu.Items.Add(sepa);


              mi = new MenuItem();

              mi.Header = "逐单元流动(CCF)转为数据集";
              mi.Command = Data2Commands.CcfToDataSet;
              mi.CommandParameter = this._collecion;

              this._contextMenu.Items.Add(mi);



              mi = new MenuItem();
              mi.Header = "流动均衡";

              mi.Command = Data2Commands.FlowBudget;
              mi.CommandParameter = this._collecion;



              this._contextMenu.Items.Add(mi);


              mi = new MenuItem();

              mi.Header = "生成向量";

              mi.Command = Data2Commands.GenerateVector;

              mi.CommandParameter = this._collecion;


              this._contextMenu.Items.Add(mi);


              
          }

      





          



          this.LoadChildren();

 
      }


   



      DSGrid3dCollecion _collecion;

      public DSGrid3dCollecion Collecion
      {
          get { return _collecion; }
          set { _collecion = value; }
      }


      public override string Desc
      {
          get
          {
              return HAUtil.LanguageTranslater.ENToCN(this._collecion.Name);
          }
      }


      protected override void LoadChildren()
      {

          foreach (DSGrid3d item in _collecion.Collecion)
          {

              DSGrid3dViewModel vm = new DSGrid3dViewModel(this,item);

              this.Children.Add(vm);

          }

          if (this._collecion is DSModflow)
          {

              DSModflow ds=this._collecion as DSModflow;

              if (ds.VectorGridData!=null)
              {


                  DSVectorGrid3dViewModel vectorVM = new DSVectorGrid3dViewModel(this, ds.VectorGridData);

                  this.Children.Add(vectorVM);


                  
              }



              ds.VectorDataChanged += new EventHandler(ds_VectorDataChanged);
          }


        


          this._collecion.DataSetCollectionChanged += new EventHandler<CollecionChangedEventArgs>(_collecion_DataSetCollectionChanged);
          
          
      }


      void ds_VectorDataChanged(object sender, EventArgs e)
      {
          DSVectorGrid3dViewModel vectorVM = this.GetVectorModel();

          if (vectorVM == null)
          {

              DSModflow ds = this._collecion as DSModflow;

              if (ds.VectorGridData != null)
              {


                  vectorVM = new DSVectorGrid3dViewModel(this, ds.VectorGridData);

                  this.Children.Add(vectorVM);


              }




          }

          else
          {

              //当存在一个vector时


              DSModflow ds = this._collecion as DSModflow;

              if (ds.VectorGridData != null)
              {

                  vectorVM.Dataset = ds.VectorGridData;

               


              }
              else
              {

                  //主数据位空

                  this.Children.Remove(vectorVM);
 

              }


          }

        
      }

      void _collecion_DataSetCollectionChanged(object sender, CollecionChangedEventArgs e)
      {
          switch (e.Type)
          {
              case CollectionChangedType.ADD:

                  DSGrid3d ds = e.Child as DSGrid3d;
                  DSGrid3dViewModel vm = new DSGrid3dViewModel(this, ds);

                  this.Children.Add(vm);


                  break;
              case CollectionChangedType.REMOVE:


                  DSGrid3d c = (DSGrid3d)e.Child;
                  DSGrid3dViewModel v = this.GetDSGridModel (c);

                  if (v != null)
                  {
                      this.Children.Remove(v);

                  }
                  
                   //UIModule.DefaultModule.ShowTimeStepView(false);



                  break;
              case CollectionChangedType.MOVE:
                  break;
              case CollectionChangedType.REPLACE:


                  break;
              case CollectionChangedType.ITEMEDIT:
                  break;
              case CollectionChangedType.RESET:
                  break;
              case CollectionChangedType.MODIFYDATATYPES:
                  break;
              default:
                  break;
          }
      }


      protected DSVectorGrid3dViewModel GetVectorModel()
      {
          foreach (MDTVIViewModel item in this.Children)
          {
              if (item is DSVectorGrid3dViewModel)
              {

                  return (DSVectorGrid3dViewModel)item;
                  
              }
              
          }

          return null;
      }


        public DSGrid3dViewModel GetDSGridModel(DSGrid3d collecion)
       {
           foreach (MDTVIViewModel item in this.Children)
           {
               if (item is DSGrid3dViewModel)
               {

                   DSGrid3dViewModel vm = (DSGrid3dViewModel)item;

                   if (vm.DsGrid  ==collecion)
                   {
                       return vm;
                       
                   }


                

               }

           }

           return null;
 
       }






      protected override void OnSelectedChanged()
      {
          if (this.IsSelected)
          {

              UIModule.DefaultModule.RibbonV2ViewModel.ViewOpViewModel.ToolVM.CurrentTool = APPTOOLMODE.GRID3D;

          }
      }

    }
}
